package com.ruoyi.web.controller.resturant;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.BizException;
import com.ruoyi.resturant.domain.ResShop;
import com.ruoyi.resturant.service.IResShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Anonymous
@RestController
@RequestMapping("/api/resturant/shop")
@Api(tags = "店铺管理")
public class ReShopController {
    @Autowired
    private IResShopService resShopService;

    @Autowired
    private RedisCache redisCache;
    @ApiOperation("取得所有店铺列表")
    @ApiImplicitParam(name = "name", value = "店铺名称",
            dataType = "string", dataTypeClass = String.class)
    @GetMapping("/list")
    public AjaxResult list(@RequestParam(value="name", required = false) String shopName) {
        resShopService.selectResShopByParam(shopName);
        List<ResShop> list = resShopService.selectResShopByParam(shopName);
        return AjaxResult.success(list);
    }
    /**
     * 获取店铺详细信息
     */
    @ApiOperation(value = "获取店铺详细信息", notes = "TODO：取得某个店铺的详细信息")
    @ApiImplicitParam(name = "id", value = "店铺ID",
            required = true, dataType = "long", paramType = "path",
            dataTypeClass = Long.class)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {

        // 1从数据库里查询
        ResShop resShop = resShopService.selectResShopById(id);
        // 2 数据库里该店铺ID不存在的场合，抛出业务异常BizException
        if(resShop == null) {
            throw new BizException(404, "该店铺id("+ id + ")不存在");
        }
        String strDB = JSON.toJSONString(resShop);
        // 3 存储到Redis数据库中
        redisCache.setCacheObject(String.valueOf(id), strDB,
                1, TimeUnit.HOURS);
        // 4 返回取得model对象
        return AjaxResult.success(resShop);

    }

    /**
     * 新增店铺
     */
    @Log(title="店铺管理",businessType = BusinessType.INSERT)
    @ApiOperation("新增店铺")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ResShop resShop) {

            return AjaxResult.success(resShopService.insertResShop(resShop));


    }

    /**
     * 修改店铺
     */
    @Log(title="店铺管理",businessType = BusinessType.UPDATE)
    @ApiOperation("修改店铺")
    @PutMapping
    public AjaxResult edit(@RequestBody ResShop resShop) {
        //1 先从根据店铺ID查询数据库，取得该店铺对象
         ResShop resShopDB=  resShopService.selectResShopById(resShop.getId());
        //2判断该店铺对象不存在，就抛出业务异常
        if(resShopDB==null) {
            throw new BizException(404, "该店铺id("+ resShop.getId() + ")不存在");
        }
        //3 如果店铺对象存在，执行更新操作
        return AjaxResult.success(resShopService.updateResShop(resShop));
    }

    /**
     * 删除店铺
     */
    @Log(title="店铺管理",businessType = BusinessType.DELETE)
    @ApiOperation("删除店铺")
    @ApiImplicitParam(name = "id", value = "店铺ID", required = true, dataType = "long", paramType = "path", dataTypeClass = Long.class)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return AjaxResult.success(resShopService.deleteResShopById(id));
    }

}
